﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;

namespace AssignmentsManage.DataClasses
{
    [XmlRootAttribute(ElementName = "GradeFraction", IsNullable = false)]
    public class GradeFraction : INotifyPropertyChanged
    {
        #region Contructors

        public GradeFraction()
        { }

        public GradeFraction(double numerator, double denominator)
        {
            Numerator = numerator;
            Denominator = denominator;
        }

        #endregion

        #region Properties

        private double _Numerator;
        [XmlElementAttribute("Numerator")]
        public double Numerator
        {
            get
            {
                return _Numerator;
            }
            set
            {
                _Numerator = value;
                OnPropertyChanged("Numerator");
                OnPropertyChanged("Percentage");
            }
        }

        private double _Denominator;
        [XmlElementAttribute("Denominator")]
        public double Denominator
        {
            get
            {
                return _Denominator;
            }
            set
            {
                _Denominator = value;
                OnPropertyChanged("Denominator");
                OnPropertyChanged("Percentage");
            }
        }

        [XmlIgnore()]
        public double Percentage
        {
            get
            {
                return Numerator / Denominator * 100;
            }
        }

        #endregion

        #region Public Methods

        public bool ConvertToNewDenominator(double newDenominator)
        {
            try
            {
                Numerator = (Denominator / newDenominator) * Numerator;
                Denominator = newDenominator;
                return true;
            }
            catch { return false; }
        }

        #endregion

        #region NotifyChanges

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
